//
//  ofSeeImageView.m
//  liaotiantupian
//
//  Created by Macx on 15/12/10.
//  Copyright (c) 2015年 Mr.D. All rights reserved.
//

#import "ofSeeImageView.h"

@interface ofSeeImageView ()
{
    UIScrollView *_imagesScrollView;
    UIImageView *_imageView;
    NSArray *_showImages;
}
@end
#define imageViewFrame CGRectMake(10, self.center.y, (kMianScreenWidth - 20), 100) ///!图片大小
@implementation ofSeeImageView

#pragma mark - init
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self moveTheView];
    }return self;
}

-(void)cresteScrollViewWithImages:(NSArray *)images withPage:(NSInteger)clickPage{
    NSInteger page = images.count;
    UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    mainScrollView.contentSize = CGSizeMake(kScreenWidth * page, kScreenHeight);
    mainScrollView.pagingEnabled = YES;
    mainScrollView.backgroundColor = [UIColor blackColor];
    [self addSubview:mainScrollView];
    
    
    for (int i = 0; i < page; i ++) {

        _imageView = [[UIImageView alloc] init];
        [_imageView sd_setImageWithURL:[NSURL URLWithString:images[i]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            _imagesScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeight)];
            
            //设置ScrollView和image是一样的大小
            [_imagesScrollView setContentSize:image.size];
            
            ///设置最大和最小缩放比例
//            _imagesScrollView.minimumZoomScale = 1.0;
//            _imagesScrollView.maximumZoomScale = 3.0;
            _imagesScrollView.delegate = self;
            [mainScrollView addSubview:_imagesScrollView];
            
            float height = image.size.height*(kScreenWidth-20)/image.size.width;

            if (height > kScreenHeight) {
                ///图片的大小
                [_imageView setFrame:CGRectMake(10, 0, kScreenWidth-20, height)
                 ];
            }else{
                [_imageView setFrame:CGRectMake(10, self.center.y - height / 2, kScreenWidth-20, height)
                 ];
            }
            [_imagesScrollView addSubview:_imageView];
        }];
    }
    // 滑動到position的位置
    CGPoint position = CGPointMake(kScreenWidth*clickPage, 0);
    [mainScrollView setContentOffset:position animated:YES];
}

///点击移除
-(void)moveTheView{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moveKeyWindow)];
    [self addGestureRecognizer:tap];
}

-(void)moveKeyWindow{
    ofSeeImageView *view = (ofSeeImageView *)[[UIApplication sharedApplication].keyWindow viewWithTag:1210];
    [view removeFromSuperview];
}

#pragma mark - scrollView delegate
//缩放过程中的图像
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    for (UIView *view in scrollView.subviews) {
        return view;
    }
    return nil;
}

//缩放结束
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    NSLog(@"缩放的比例:%f",scale);
}

//缩放中
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"缩放中");
}
@end
